<#--
备注:
- 当前文件下存储mysql可以使用的代码
-->
    <insert id="insert">
<#if idType??>
    <#if idType == "MYSQL_AUTO_INCREMENT">
        <selectKey keyColumn="${pkEntity.columnName?upper_case}" resultType="int" keyProperty="${entityName?uncap_first}.${pkEntity.javaFiledName}" order="AFTER">
            SELECT LAST_INSERT_ID()
        </selectKey>
    </#if>
</#if>
        insert into ${tableInfo.tableName}
        (
<#list columnEntityList as columnTemp>
            ${columnTemp.columnName?upper_case}<#if columnTemp_has_next>,</#if>
</#list>
        )
        values
        (
<#list columnEntityList as columnTemp>
            <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}, jdbcType=${columnTemp.jdbcType}<#noparse>}</#noparse><#if columnTemp_has_next>,</#if>
</#list>
        )
    </insert>

    <insert id="insertBatch">
        insert into ${tableInfo.tableName}
        (
        <#list columnEntityList as columnTemp>
            ${columnTemp.columnName?upper_case}<#if columnTemp_has_next>,</#if>
        </#list>
        )
        values
        <foreach item="item" collection="${entityName?uncap_first}s" separator="," index="index">
        (
        <#list columnEntityList as columnTemp>
            <#noparse>#{</#noparse>item.${columnTemp.javaFiledName}, jdbcType=${columnTemp.jdbcType}<#noparse>}</#noparse><#if columnTemp_has_next>,</#if>
        </#list>
        )
        </foreach>
    </insert>
    <#--
    备注:
    - 当前文件下存储所有数据库都可以使用的公共代码
    -->
    <resultMap id="${entityName}" type="${entityImportInfo}">
        <#list columnEntityList as columnTemp>
            <#if columnTemp.columnKey?? && columnTemp.columnKey == 'PRI'>
        <id property="${columnTemp.javaFiledName}" column="${columnTemp.columnName?upper_case}" /> <!-- ${columnTemp.columnComment} -->
            <#else>
        <result property="${columnTemp.javaFiledName}" column="${columnTemp.columnName?upper_case}" /> <!-- ${columnTemp.columnComment} -->
            </#if>
        </#list>
    </resultMap>

<#if deleteMode == 'TOMBSTONE'>
    <#--逻辑删除未删除的sql片段, 例如: deleted = 'N'-->
    <#assign tombstoneNoDeletedEqualsSqlName="tombstoneWhereConditionSql">
    <sql id="${tombstoneNoDeletedEqualsSqlName}">
        ${tableAlias}.${tombstoneConfig.tombstoneColumnName} = <#if tombstoneConfig.tombstoneColumnType == 'STRING' || tombstoneConfig.tombstoneColumnType == 'CHAR'>'</#if>${tombstoneConfig.notDeletedValue}<#if tombstoneConfig.tombstoneColumnType == 'STRING' || tombstoneConfig.tombstoneColumnType == 'CHAR'>'</#if>
    </sql>
</#if>

    <update id="updateById">
        update ${tableInfo.tableName} ${tableAlias}
        <set>
            <#list columnEntityList as columnTemp>
            <#--主键不需要设置到动态条件中-->
                <#if columnTemp.javaFiledName != pkEntity.javaFiledName>
                    <#if columnTemp.javaTypeName == 'String'>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null and ${entityName?uncap_first}.${columnTemp.javaFiledName} != ""'>
                ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>,
            </if>
                    <#else>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null'>
                ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>,
            </if>
                    </#if>
                </#if>
            </#list>
        </set>
        where ${tableAlias}.${pkEntity.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${pkEntity.javaFiledName}<#noparse>}</#noparse>
        <#if deleteMode == 'TOMBSTONE'>
            AND <include refid="${tombstoneNoDeletedEqualsSqlName}" />
        </#if>
    </update>

    <delete id="deleteById">
    <#if deleteMode == 'TOMBSTONE'>
        update ${tableInfo.tableName} ${tableAlias}
        set <include refid="${tombstoneNoDeletedEqualsSqlName}" />
        where ${tableAlias}.${pkEntity.columnName?upper_case} = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    <#else>
        delete from ${tableInfo.tableName} ${tableAlias}
        where ${tableAlias}.${pkEntity.columnName?upper_case} = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
    </#if>
    </delete>

    <delete id="deleteBatchIds">
        <#if deleteMode == 'TOMBSTONE'>
        update ${tableInfo.tableName} ${tableAlias}
        set <include refid="${tombstoneNoDeletedEqualsSqlName}" />
        where
        <choose>
            <when test="idList != null and idList.size() > 0">
                id in
                <foreach collection="idList" open="(" close=")" item="id" index="id_index" separator=",">
                    <#noparse>#{</#noparse>id<#noparse>}</#noparse>
                </foreach>
            </when>
            <otherwise>
                1 = 2
            </otherwise>
        </choose>
        <#else>
        delete from ${tableInfo.tableName} ${tableAlias}
        where
        <choose>
            <when test="idList != null and idList.size() > 0">
                id in
                <foreach collection="idList" open="(" close=")" item="id" index="id_index" separator=",">
                    <#noparse>#{</#noparse>id<#noparse>}</#noparse>
                </foreach>
            </when>
            <otherwise>
                1 = 2
            </otherwise>
        </choose>
        </#if>
    </delete>

    <select id="selectById" resultMap="${entityName}">
        select
        <#list columnEntityList as columnTemp>
            ${tableAlias}.${columnTemp.columnName?upper_case}<#if columnTemp_has_next>,</#if>
        </#list>
        from ${tableInfo.tableName} ${tableAlias}
        where ${tableAlias}.${pkEntity.columnName?upper_case} = <#noparse>#{</#noparse>id<#noparse>}</#noparse>
        <#if deleteMode == 'TOMBSTONE'>
            AND <include refid="${tombstoneNoDeletedEqualsSqlName}" />
        </#if>
    </select>

    <select id="selectBatchIds" resultMap="${entityName}">
        select
        <#list columnEntityList as columnTemp>
            ${tableAlias}.${columnTemp.columnName?upper_case}<#if columnTemp_has_next>,</#if>
        </#list>
        from ${tableInfo.tableName} ${tableAlias}
        where ${tableAlias}.${pkEntity.columnName?upper_case} in
        <foreach collection="idList" item="id" index="index" separator="," open="(" close=")">
            <#noparse>#{</#noparse>id<#noparse>}</#noparse>
        </foreach>
        <#if deleteMode == 'TOMBSTONE'>
            AND <include refid="${tombstoneNoDeletedEqualsSqlName}" />
        </#if>
    </select>

    <select id="selectByDynamicCondition" resultMap="${entityName}">
        select
        <#list columnEntityList as columnTemp>
            ${tableAlias}.${columnTemp.columnName?upper_case}<#if columnTemp_has_next>,</#if>
        </#list>
        from ${tableInfo.tableName} ${tableAlias}
        <where>
            <#if deleteMode == 'TOMBSTONE'>
                <include refid="${tombstoneNoDeletedEqualsSqlName}" />
            </#if>
            <#list columnEntityList as columnTemp>
                <#if columnTemp.javaTypeName == 'String'>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null and ${entityName?uncap_first}.${columnTemp.javaFiledName} != ""'>
                AND ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>
            </if>
                <#else>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null'>
                AND ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>
            </if>
                </#if>
            </#list>
        </where>
    </select>

    <select id="countByDynamicCondition" resultType="int">
        select
            count(1)
        from ${tableInfo.tableName} ${tableAlias}
        <where>
            <#if deleteMode == 'TOMBSTONE'>
                <include refid="${tombstoneNoDeletedEqualsSqlName}" />
            </#if>
            <#list columnEntityList as columnTemp>
                <#if columnTemp.javaTypeName == 'String'>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null and ${entityName?uncap_first}.${columnTemp.javaFiledName} != ""'>
                and ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>
            </if>
                <#else>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null'>
                and ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>
            </if>
                </#if>
            </#list>
        </where>
    </select>

    <select id="existsByDynamicCondition" resultType="java.lang.Boolean">
        select
            count(1)
        from ${tableInfo.tableName} ${tableAlias}
        <where>
            <#if deleteMode == 'TOMBSTONE'>
                <include refid="${tombstoneNoDeletedEqualsSqlName}" />
            </#if>
            <#list columnEntityList as columnTemp>
                <#if columnTemp.javaTypeName == 'String'>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null and ${entityName?uncap_first}.${columnTemp.javaFiledName} != ""'>
                and ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>
            </if>
                <#else>
            <if test='${entityName?uncap_first}.${columnTemp.javaFiledName} != null'>
                and ${tableAlias}.${columnTemp.columnName?upper_case} = <#noparse>#{</#noparse>${entityName?uncap_first}.${columnTemp.javaFiledName}<#noparse>}</#noparse>
            </if>
                </#if>
            </#list>
        </where>
    </select>
